package cn.openea.perpartner.sysuser.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import cn.openea.perpartner.common.auth.login.LoginService;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.system.entity.SysUser;
import cn.openea.perpartner.system.entity.SysUserUnion;
import cn.openea.perpartner.system.service.ISysUserService;
import cn.openea.perpartner.system.service.ISysUserUnionService;
import cn.openea.perpartner.sysuser.domain.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 个人信息 业务处理
 */
@Api(tags = "个人信息")
@Controller
@RequestMapping("/system/user/profile")
public class SysProfileController extends BaseController {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysUserUnionService unionService;
    @Autowired
    private LoginService loginService;

    /**
     * 个人信息
     */
    @ApiOperation("用户详情")
    @GetMapping("/{userId}/{appId}")
    @ResponseBody
    public AjaxResult profile(@PathVariable String userId,@PathVariable String appId) {
        SysUser user = userService.findUser(userId);
        if (BeanUtil.isEmpty(user)) {
            return error("用户不存在");
        }
        if (StrUtil.isBlank(appId)){
            return error("系统id不能为空");
        }
        SysUserUnion union = unionService.selectByUserId(userId, user.getUnionid(),appId);
        UserVo vo = new UserVo();
        BeanUtil.copyProperties(user, vo);
        vo.setOpenId(union.getOpenid());
        return success(vo);
    }

    @ApiOperation("修改密码")
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwd(String userId, String oldPassword, String newPassword, HttpServletRequest request, HttpServletResponse response) {
        SysUser user = userService.findUser(userId);
        if (!BCrypt.checkpw(oldPassword, user.getPassword())) {
            return error("修改密码失败，旧密码错误");
        }
        if (BCrypt.checkpw(newPassword, user.getPassword())) {
            return error("新密码不能与旧密码相同");
        }
        user.setPassword(BCrypt.hashpw(newPassword, BCrypt.gensalt()));
        if (userService.saveOrUpdate(user)) {
            loginService.logout(request, response);
            return success("修改密码成功");
        }
        return error("修改密码异常，请联系管理员");
    }

    @ApiOperation("更新详情")
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult edit(@RequestBody SysUser user) {
        return userService.updateUserInfo(user) ? success(user) : error();
    }
    @ApiOperation("重置密码")
    @PostMapping("/reset/pwd")
    @ResponseBody
    public AjaxResult reset(@RequestBody SysUser user) {

        return toAjax(userService.resetPwd(user.getUserid(), user.getPassword()));
    }

}
